package com.duobaoyu.dby.supply.admin.controller.message;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.admin.converter.SupplyChainAdminConverter;
import com.duobaoyu.dby.supply.biz.annotation.TableQueryHead;
import com.duobaoyu.dby.supply.biz.service.message.MessageService;
import com.duobaoyu.dby.supply.common.biz.dto.message.ChannelMessageDTO;
import com.duobaoyu.dby.supply.common.biz.dto.message.ChannelMessageStatusDTO;
import com.duobaoyu.dby.supply.common.biz.vo.message.ChannelMessageVO;
import com.duobaoyu.dby.supply.db.entity.message.ChannelMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName ChannelMessageController
 * @Description 渠道消息记录Controller
 * @createTime 13:48:00 2023/11/08
 */
@RestController
@RequiredArgsConstructor
@TableQueryHead(key = "Table-Suffix")
@RequestMapping("/admin/channel-message")
@Api(value = "channel-message", tags = "渠道消息")
public class ChannelMessageController {

    private final MessageService messageService;
    private final SupplyChainAdminConverter supplyChainAdminConverter;

    /**
     * 渠道消息分页查询
     * @param channelMessage
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询渠道消息信息", notes = "分页查询渠道消息信息")
    @SupplySysLog(value = "分页查询渠道消息信息",operationType = OperationTypeEnum.SELECT)
    public R<Page<ChannelMessageVO>> channelMessagePage(ChannelMessageDTO channelMessage) {
        Page<ChannelMessage> page = messageService.channelMessagePage(channelMessage);
        return R.ok(supplyChainAdminConverter.toChannelMessagePage(page));
    }

    /**
     * 修改渠道消息状态
     * @param channelMessageStatus
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改渠道消息状态", notes = "修改渠道消息状态")
    @SupplySysLog(value = "修改渠道消息状态",operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> updateChannelMessageStatus(@Validated @RequestBody ChannelMessageStatusDTO channelMessageStatus) {
        Boolean result = messageService.updateChannelMessageStatus(channelMessageStatus);
        return R.ok(result);
    }

    /**
     * 渠道消息手动执行业务逻辑
     * @param channelMessageId
     * @return
     */
    @PutMapping("/handler/{channelMessageId}")
    @ApiOperation(value = "渠道消息手动执行业务逻辑", notes = "渠道消息手动执行业务逻辑")
    @SupplySysLog(value = "渠道消息手动执行业务逻辑",operationType = OperationTypeEnum.UPDATE)
    public R<Boolean> channelMessageHandler(@NotBlank(message = "渠道消息Id不能为空") @PathVariable("channelMessageId") String channelMessageId) {
        Boolean result = messageService.channelMessageHandler(channelMessageId);
        return R.ok(result);
    }
}
